﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MWS.Processing.Interfaces;

namespace MWS.Processing.Trackers
{
    
    public class MemoryRequestTracker : IRequestTracker 
    {
        private readonly IDictionary<String, DateTime> requestRepository = new Dictionary<String, DateTime>();

        public TimeSpan TrackingWindow { get; set; }

        public void RecordRequests(IList<string> requestKeys)
        {
            foreach (var requestKey in requestKeys)
            {
                if (requestRepository.ContainsKey(requestKey))
                    requestRepository[requestKey] = DateTime.UtcNow;
                else
                    requestRepository.Add(requestKey, DateTime.UtcNow);
            }
        }

        public IDictionary<string, DateTime> GetRequests(TimeSpan timeFilter)
        {
            return requestRepository
                .Where(r => r.Value >= DateTime.UtcNow.Subtract(timeFilter))
                .ToDictionary(r => r.Key, r => r.Value);
        }
    }
}
